#include "circleloopdrawable.h"
#include <math.h>
CircleLoopDrawable::CircleLoopDrawable()
{
}

CircleLoopDrawable::CircleLoopDrawable(QPointF center, double radius)
{
    _center = center;
    _radius = radius;
    resetColor();
}

void CircleLoopDrawable::draw(CoodrsTransformer *coordsTransformer)
{
    glLineWidth(LINE_WIDTH);
    glBegin(GL_LINE_STRIP);
    glColor3f(_color.redF(), _color.greenF(), _color.blueF());
    for(int i=0; i<=360; i+=36)
    {
        QPointF coords;
        coords.setX(_center.x() + _radius*cos(i*PI/180.0));
        coords.setY(_center.y() + _radius*sin(i*PI/180.0));
        if(coordsTransformer)
        {
            coords = coordsTransformer->transform(coords);
        }

        glVertex2d(coords.x(), coords.y());
    }
    glEnd();
    glLineWidth(1);
}

QRectF CircleLoopDrawable::getBoundingBox()
{
    QPointF topLeft = QPointF((_center.x() - _radius), (_center.y() - _radius));
    QPointF rightBottom = QPointF((_center.x() + _radius), (_center.y() + _radius));
    return QRectF(topLeft, rightBottom);
}

bool CircleLoopDrawable::_pointInCircle(QPointF point)
{
    double x = point.x() - _center.x();
    double y = point.y() - _center.y();
    double R = _radius;
    return ((x*x + y*y) <= R*R );
}

bool CircleLoopDrawable::isSelected(QPointF selectedPoint)
{
    QRectF boundary = getBoundingBox();
    if(boundary.contains(selectedPoint))
    {
        if(_pointInCircle(selectedPoint))
        {
            return true;
        }
    }
    return false;
}

